﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameStateManagement;
using BHG.System;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Controllers;
using FarseerPhysics.Dynamics.Contacts;
using FarseerPhysics.Collision.Shapes;
using FarseerPhysics.Factories;
using FarseerPhysics.Common;



namespace BHG.GameData
{
    class Level03 : GameplayScreen
    {
        bool playBoat = false;
        Model greenFloor;
        Model krychleDrevo;
        Model ship;
        GraphicObject shipObject;
        Model strom;
        GraphicObject[] stromy;
        float[] stromyChange;
        float[] maxStromyChange;
        Song song;

        bool skokZLetadla = false;

        float cubesMakerTimer = 1;

        Model airbus1;
        GraphicObject airbusObject;

        SoundEffect f16, boat;

        public override void LoadContent()
        {
            base.LoadContent();
            MediaPlayer.Stop();

            f16 = content.Load<SoundEffect>("Sound/sounds/F-15Taxi");
            boat = content.Load<SoundEffect>("Sound/sounds/5boat");
            airbus1 = content.Load<Model>("Models/Planes/airbus1");
            greenFloor = content.Load<Model>("Models/Lands/Grass/grass1");
            krychleDrevo = content.Load<Model>("Models/Cubes/krychle-drevo");
            strom = content.Load<Model>("Models/Backgrounds/Trees/strom1");
            ship = content.Load<Model>("Models/Ships/ship1");

            player.Position = new Vector2(200, 60);
            this.BaseLand(0, 0, 8);
            this.Cubes(15, 15, 10);
            song = content.Load<Song>("Sound/music/08 Collapsed 118 Master");
            this.BaseLand(70, 50, 10);
            this.BaseLand(200, 50, 10);
            this.BaseLand(340, 50, 10);
            this.trees();
            this.CreateShowInfobar();
            this.Airbus(-1500, 0);
            this.Ships(170,50);
            airbusObject.SpeedPerSecond = new Vector3(100, 0, 0);
            gravityGunAllowed = true;
            blackHoleGunAllowed = true;
            MediaPlayer.Play(song);
            f16.Play();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            // generic update, update physics, call only once
            base.Update(gameTime, otherScreenHasFocus, false);


            cubesMakerTimer -= (float)gameTime.ElapsedGameTime.TotalSeconds;
            
            if (cubesMakerTimer < 0)
            {
                cubesMakerTimer = 0.1f;
                CreateFastCube(310, 60);
            }

            if (player.Position.X > 120 && player.Position.X < 200 && playBoat == false)
            {
                playBoat = true;
                boat.Play();
            }

            if (shipObject.Depth < -200)
            {
                shipObject.SpeedPerSecond = new Vector3(0, 0, 50);
                playBoat = false;
            }
            if (shipObject.Depth > 100)
            {
                shipObject.SpeedPerSecond = new Vector3(0, 0, -50);
            }

            if (player.Position.X >= 170 && player.Position.X <= 185 && shipObject.Depth > - 50)
            {
                Death();
            }

            if (player.Position.Y < -50)
            {
                Death();
            }

            if (camera.distance > 60)
            {
                camera.distance -= 1;
            }


            if (player.Position.X > 440)
            {
                LoadingScreen.Load(ScreenManager, true, PlayerIndex.One, CurrentGame.createNewLevel(4));
            }

            if (airbusObject.Position.X > -50)
            {
                if (skokZLetadla == false)
                {
                    player.body.LinearVelocity = new Vector2(30, 30);
                    skokZLetadla = true;
                }
            }
            else
            {
                player.Position = new Vector2(airbusObject.Position.X+30, airbusObject.Position.Y-3);
                camera.distance = 400;
            }

            for (int i = 0; i < stromy.Length; i++)
            {
                stromyChange[i] -= (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (stromyChange[i] <= 0)
                {
                    stromyChange[i] = maxStromyChange[i];
                    stromy[i].SpeedPerSecond = new Vector3(stromy[i].SpeedPerSecond.X * -1, stromy[i].SpeedPerSecond.Y * -1, stromy[i].SpeedPerSecond.Z * -1);
                }
            }
            this.UpdateShowInfobar(player.Position.X, player.Position.Y);
        }

        public void CreateFastCube(float x, float y)
        {
            CubeObject tempObject = new CubeObject(
                new Vector2(x, y),
                new Vector2(5, 5),
                true,
                krychleDrevo

                );
            tempObject.Scale = 2.5f;
            tempObject.body.LinearVelocity = new Vector2(-30,0);
            tempObject.Life = 1;
            objectsOnMap.Add(tempObject);
            
        }

        public void Ships(float x, float y)
        {
            shipObject = new GraphicObject(
                new Vector2(x, y),
                ship,
                0);
            shipObject.Scale = 0.01f;
            shipObject.SpeedPerSecond = new Vector3(0, 0, -100);
            objectsOnMap.Add(shipObject);
        }

        public void Death()
        {
            LoadingScreen.Load(ScreenManager, true, PlayerIndex.One, CurrentGame.createNewLevel(3));
        }

        public void trees()
        {
            Random random = new Random();
            stromy = new GraphicObject[20];
            stromyChange = new float[stromy.Length];
            maxStromyChange = new float[stromy.Length];
            for (int i = 0; i < stromy.Length; i++)
            {
                maxStromyChange[i] = (float)random.NextDouble() * 5 + 5;
                stromyChange[i] = maxStromyChange[i];
                stromy[i] = new GraphicObject(
                    new Vector2(random.Next(1000), -100 + random.Next(200)),
                    strom,
                    -50 - random.Next(1000)
                    );
                stromy[i].RotationPerSecond = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
                stromy[i].SpeedPerSecond = new Vector3((float)random.NextDouble() * 50, (float)random.NextDouble() * 50, (float)random.NextDouble() * 50);
                objectsOnMap.Add(stromy[i]);
            }
        }

        public void Cubes(float posX, float posY, int count)
        {
            for (int i = 0; i < count; i++)
            {
                CubeObject tempCube = new CubeObject(
                    new Vector2(posX + i * 5, posY),
                    new Vector2(5, 5),
                    true,
                    krychleDrevo);
                objectsOnMap.Add(tempCube);
                tempCube.Scale = 2.5f;
            }
            for (int i = 0; i < 10; i++)
            {
                CubeObject tempCube = new CubeObject(
                    new Vector2(posX + count * 5, posY + i * 5 - 10),
                    new Vector2(5, 5),
                    false,
                    krychleDrevo);
                objectsOnMap.Add(tempCube);
                tempCube.Scale = 2.5f;
            }

        }

        /// <summary>
        /// Create the base objects where you can walk
        /// </summary>
        /// <param name="positionX"></param>
        /// <param name="positionY"></param>
        public void BaseLand(float positionX, float positionY, int count)
        {

            //Here you can walk
            float thick = 10f;
            float thickUp = 1f;
            for (int i = 0; i < count; i++)
            {
                objectsOnMap.Add(new CubeObject(
                    new Vector2(
                        positionX + i * thick,
                        positionY),
                    new Vector2(
                        thick,
                        thickUp),
                    false,
                    greenFloor
                    ));
            }
        }

        public void Airbus(float x, float y)
        {
            airbusObject = new GraphicObject(
                new Vector2(x, y),
                airbus1,
                0f
                );
            airbusObject.Scale = 0.1f;
            airbusObject.Depth = 18;
            objectsOnMap.Add(airbusObject);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            if (airbusObject.Position.X < 0)
            {
                ScreenManager.SpriteBatch.Begin();
                ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "I was flying about 10 hours in nowhere...", new Vector2(40, 40), Color.Red);
                ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "I had dream about man, who told me, that he increased my power...", new Vector2(40, 80), Color.Red);
                ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "He told something about Gravity Gun and left mouse button.", new Vector2(40, 120), Color.Red);
                ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "What is left mouse button?", new Vector2(40, 160), Color.Red);
                ScreenManager.SpriteBatch.End();
            }
            if (player.Position.X > 400)
            {
                ScreenManager.SpriteBatch.Begin();
                ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "I have to jump down now.", new Vector2(40, 160), Color.Red);
                ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, "I can feel it...", new Vector2(40, 200), Color.Red);
                ScreenManager.SpriteBatch.End();
            }
        }

    }
}
